//
//  LoginViewController.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/11/13.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SVProgressHUD
class LoginViewController: UIViewController {

    @IBOutlet weak var enterBackView: UIView!
    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.navigationController?.delegate = self
        setEnterBackViewStyle()
    }
    
    private func setEnterBackViewStyle(){
        enterBackView.layer.cornerRadius = 10.0
        enterBackView.layer.masksToBounds = false   //如果是true，阴影不显示
        enterBackView.layer.shadowColor = UIColor.init(red: 235/255.0, green: 232/255.0, blue: 235/255.0, alpha: 1).cgColor
        enterBackView.layer.shadowOpacity = 1.0
        enterBackView.layer.shadowRadius = 10.0
        enterBackView.layer.shadowOffset = CGSize(width: 0, height: 0)
    }
    
    @IBAction func login(_ sender: Any) {
        SVProgressHUD.show()
        let path = "user/login.do"
        let params = ["account":usernameTF.text,"password":passwordTF.text]
        print(path)
        print(params)
        AFNetWorkManager.shareNetworkTools().post(path, parameters: params, success: { (_, JSON) in
            SVProgressHUD.dismiss()
            let dict = JSON as![String:AnyObject]
            let dictStatus = dict["status"]as!NSNumber
            if dictStatus.intValue == 0{
                let dictData = dict["data"]
                let token = dictData?["token"]
                var userDic = dictData?["user"] as! [String:AnyObject]
                let deviceDics = dictData?["devices"] as! [[String:AnyObject]]
                userDic.updateValue(token as AnyObject, forKey: "token")
                var devices = [BBDevice]()
                for dic in deviceDics{
                    let device = BBDevice.init(dict: dic)
                    devices.append(device)
                }
                userDic.updateValue(devices as AnyObject, forKey: "devices")
                let user = BBUser.init(dict: userDic )
                
                user.saveUser()
                SVProgressHUD.showSuccess(withStatus: "Login Success!")
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: BBSwitchRootViewControllerKey), object: false)
            }
        }) { (_, error) in
            SVProgressHUD.dismiss()
            SVProgressHUD.showError(withStatus: "Login Failed!")
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        usernameTF.resignFirstResponder()
        passwordTF.resignFirstResponder()
    }
    
    @IBAction func close(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func forgetPassword(_ sender: Any) {
        self.performSegue(withIdentifier: "forget", sender: nil)
    }
    
    @IBAction func signUp(_ sender: Any) {
        self.performSegue(withIdentifier: "signup", sender: nil)
    }
    
}


